#include <iostream>
#include <map>
#include <iomanip>

using namespace std;

const int BROADCAST_ADDRESS = 0xF;
const int PORT_COUNT = 3;

bool isValidPort(int port) {
    return port >= 1 && port < PORT_COUNT;
}

int findOutputPort(const map<int, int>& mac_table, int src_port, int dest_addr) {
    if (dest_addr == BROADCAST_ADDRESS) {
        return PORT_COUNT - src_port;
    }
    auto it = mac_table.find(dest_addr);
    if (it != mac_table.end() && it->second != src_port) {
        return it->second;
    }
    return PORT_COUNT - src_port;
}

void printMacTable(const map<int, int>& mac_table) {
    cout << " MAC Table: ";
    for (const auto& entry : mac_table) {
        cout << "0x" << hex << setw(1) << entry.first
            << ":" << dec << entry.second << " ";
    }
    cout << endl;
}

int main() {
    map<int, int> mac_table;
    int src_addr, src_port, dest_addr;

    while (cin >> hex >> src_addr >> src_port >> hex >> dest_addr) {
        if (!isValidPort(src_port)) {
            cerr << "Error: Invalid port number " << src_port << endl;
            continue;
        }

        mac_table[src_addr] = src_port;

        int out_port = findOutputPort(mac_table, src_port, dest_addr);

        if (out_port != -1) {
            cout << out_port;
        }

        printMacTable(mac_table);
    }

    return 0;
}